package com.sushi.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sushi.enums.Status;
import com.sushi.exception.BusinessException;
import com.sushi.repository.entity.CommentEntity;
import com.sushi.repository.mapper.CommentMapper;
import com.sushi.response.FileResp;
import com.sushi.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sushi.response.ResponseBO;
import com.sushi.response.ResponsePageBO;
import com.sushi.response.CommentResp;
import com.sushi.request.CommentReq;
import com.sushi.request.CommentPageReq;
import com.sushi.service.FileService;
import com.sushi.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论 服务实现类
 * @date 2023-05-02
 * @time 17:13:38
 * @author Administrator
 */
@Slf4j
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentEntity> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private FileService fileService;

    

    /**
     * 新增
     */
    @Override
    public ResponseBO<CommentResp> add(CommentReq req) {
        CommentEntity entity = BeanUtil.convert(req, CommentEntity.class);
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("保存失败");
        }
        entity.setStatus(Status.Comment.NORMAL.getCode());
        entity.setUserId(UserUtil.getUserId());
        entity.setCreatorId(UserUtil.getUserId());
        entity.setCreatorName(UserUtil.getUserName());
        entity.insert();
        return ResponseUtil.ok(BeanUtil.convert(entity, CommentResp.class));
    }

    /**
     * 逻辑删除
     */
    @Override
    public ResponseBO<CommentResp> logicRemove(CommentReq req) {
        if (StringUtil.isBlank(req.getId())) {
            throw new BusinessException("id不能为空");
        }
        CommentEntity entity = new CommentEntity();
        entity.setId(req.getId());
        entity.setDeleted(1L);
        entity.updateById();
        return ResponseUtil.ok(BeanUtil.convert(entity, CommentResp.class));
    }

    /**
     * 更新
     */
    @Override
    public ResponseBO<CommentResp> updateByField(CommentReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        CommentEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            return ResponseUtil.error("信息不存在，无法更新");
        }
        if (StringUtil.isNotBlank(req.getId())) {
            entity.setId(req.getId());
        }
        if (StringUtil.isNotBlank(req.getPostId())) {
            entity.setPostId(req.getPostId());
        }
        if (StringUtil.isNotBlank(req.getParentId())) {
            entity.setParentId(req.getParentId());
        }
        if (StringUtil.isNotBlank(req.getStatus())) {
            entity.setStatus(req.getStatus());
        }
        if (StringUtil.isNotBlank(req.getContent())) {
            entity.setContent(req.getContent());
        }
        if (StringUtil.isNotBlank(req.getUserId())) {
            entity.setUserId(req.getUserId());
        }
        if (StringUtil.isNotBlank(req.getUserName())) {
            entity.setUserName(req.getUserName());
        }
        if (StringUtil.isNotBlank(req.getFileUrl())) {
            entity.setFileUrl(req.getFileUrl());
        }
        if (StringUtil.isNotBlank(req.getPictureUrl())) {
            entity.setPictureUrl(req.getPictureUrl());
        }

        entity.updateById();
        log.info("更新成功：{}", JSON.toJSONString(entity));
        return ResponseUtil.ok(BeanUtil.convert(entity, CommentResp.class));
    }

    /**
     * 列表
     */
    @Override
    public ResponsePageBO<CommentResp> listByCondition(CommentPageReq req) {
        if (ObjectUtil.isEmpty(req) || ObjectUtil.isEmpty(req.getPageNum()) || ObjectUtil.isEmpty(req.getPageSize())) {
            throw new BusinessException("必要参数不能为空");
        }
        LambdaQueryWrapper<CommentEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommentEntity::getDeleted, 0L);
        wrapper.eq(StringUtil.isNotBlank(req.getId()), CommentEntity::getId, req.getId());
        wrapper.eq(StringUtil.isNotBlank(req.getPostId()), CommentEntity::getPostId, req.getPostId());
        wrapper.eq(StringUtil.isNotBlank(req.getParentId()), CommentEntity::getParentId, req.getParentId());
        wrapper.eq(StringUtil.isNotBlank(req.getStatus()), CommentEntity::getStatus, req.getStatus());
        wrapper.eq(StringUtil.isNotBlank(req.getContent()), CommentEntity::getContent, req.getContent());
        wrapper.eq(StringUtil.isNotBlank(req.getUserId()), CommentEntity::getUserId, req.getUserId());
        wrapper.eq(StringUtil.isNotBlank(req.getUserName()), CommentEntity::getUserName, req.getUserName());
        wrapper.eq(StringUtil.isNotBlank(req.getFileUrl()), CommentEntity::getFileUrl, req.getFileUrl());
        wrapper.eq(StringUtil.isNotBlank(req.getPictureUrl()), CommentEntity::getPictureUrl, req.getPictureUrl());

        Page<CommentEntity> page = page(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
        if (ObjectUtil.isEmpty(page) || CollectionUtil.isEmpty(page.getRecords())) {
            return ResponsePageUtil.ok(req, new ArrayList<>(), 0L);
        }
        List<CommentResp> convert = BeanUtil.convert(page.getRecords(), CommentResp.class);
        return ResponsePageUtil.ok(req, convert, page.getTotal());
    }

    /**
     * 详情
     */
    @Override
    public ResponseBO<CommentResp> detail(CommentReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        CommentEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("对象不存在");
        }
        return ResponseUtil.ok(BeanUtil.convert(entity, CommentResp.class));
    }

    @Override
    public void fillChildren(List<CommentResp> commentTops) {
        if (CollectionUtil.isEmpty(commentTops)) {
            return;
        }
        commentTops.forEach(comment -> {
            CommentEntity self = getById(comment.getId());
            if (StringUtil.isNotBlank(self.getFileUrl())) {
                List<String> list = StringUtil.getList(self.getFileUrl());
                List<FileResp> fileList = fileService.listByIdList(list);
                comment.setFileList(fileList);
            }
            if (StringUtil.isNotBlank(self.getPictureUrl())) {
                List<String> list = StringUtil.getList(self.getPictureUrl());
                List<FileResp> picList = fileService.listByIdList(list);
                comment.setPictureList(picList);
            }

            List<CommentEntity> childrenEntities = list(new LambdaQueryWrapper<CommentEntity>().eq(CommentEntity::getDeleted, 0L).eq(CommentEntity::getParentId, comment.getParentId()));
            List<CommentResp> childrenList = BeanUtil.convert(childrenEntities, CommentResp.class);
            if (CollectionUtil.isNotEmpty(childrenList)){
                comment.setChildren(childrenList);
                fillChildren(childrenList);
            }else {
                comment.setChildren(new ArrayList<>());
            }
        });
    }

    @Override
    public List<CommentResp> getPostComments(String postId) {
        List<CommentEntity> topList = list(new LambdaQueryWrapper<CommentEntity>().eq(CommentEntity::getDeleted, 0L).eq(CommentEntity::getPostId, postId).isNull(CommentEntity::getParentId));
        List<CommentResp> convert = BeanUtil.convert(topList, CommentResp.class);
        fillChildren(convert);
        return CollectionUtil.isEmpty(convert) ? new ArrayList<>() : convert;
    }

    ;

}